const app = getApp();
Page({
    data: {
        id: "",
        secondPeelingType: "",
        role: "",
        cropName: "",
        farmerMobile: "",
        companyName: "",
        farmerName: "",
        qualityType: null,
        enterStockStatus: null,
        createTimeStr: "",
        grossWeight: "",
        tareWeight: "",
        carWeight: "",
        netWeight: 0,
        price: "",
        managementFee: "",
        allPrice: 0,
        activeNames: ["collapse"],
        typeList: [],
        qualityType: "",
        errMessage: {
            "carWeight": "请填写皮重",
            "grossWeight": "请填写毛重",
            "packs": "请选择二次皮重",
            "qualityType": "请选择商品品质",
            "tareWeight": "请选择二次皮重"
        },
        peelingTypeMap: {
            1: "筐",
            2: "次品",
            3: "次品+筐",
        },
        statusMap: {
            0: "待除皮",
            1: "待除皮",
            2: "待开价",
            3: "待结账",
            4: "已结算",
        },
        statusMapColor: {
            0: "green",
            1: "green",
            2: "yellow",
            3: "red",
            4: "grey"
        },
        qualityMap: {
            1: "精品",
            2: "次品",
            3: "统货",
        },
        operateRecord: []
    },
    onLoad: function (options) {
        this.setData({
            id: options.id,
            role: app.globalData.userInfo.role
        })
        this.getData(this.data.id)
    },
    onShow: function () {
        wx.getStorage({
            key: 'tareWeightType',
            success: (res) => {
                let typeList = res.data.typeList
                let qualityType = res.data.qualityType
                let secondPeelingType = res.data.secondPeelingType
                if (typeList && typeList.length) {
                    let tareWeight = 0
                    typeList.forEach(item => {
                        tareWeight += item.packWeight * 1
                    });
                    this.setData({
                        typeList: typeList,
                        tareWeight: tareWeight
                    })
                    this.setNetWeight()
                } else {
                    this.setData({
                        typeList: [],
                        tareWeight: 0
                    })
                    this.setNetWeight()
                }
                this.setData({
                    qualityType: qualityType,
                    secondPeelingType: secondPeelingType,
                })
                wx.removeStorage({
                    key: 'tareWeightType',
                })
            }
        })
    },
    getData(id) {
        wx.showLoading({
            title: "正在请求数据中"
        })
        wx.$apis.getMarketDetail(id).then(res => {
            let typeList = res.data.packs || []
            // 操作记录
            let operateRecord = [];
            if (res.data.offerPriceOperator && this.data.role != "2") {
                operateRecord.push({
                    time: res.data.offerPriceTime.replace("T", " "),
                    operator: res.data.offerPriceOperatorName || "",
                    role: "管理员",
                    actionType: "出价",
                })
            }
            if (res.data.peelingOperator) {
                operateRecord.push({
                    time: res.data.peelingTime.replace("T", " "),
                    operator: res.data.peelingOperatorName || "",
                    role: "小工",
                    actionType: "二次除皮",
                })
            }
            operateRecord.push({
                time: res.data.createTime.replace("T", " "),
                operator: res.data.farmerName || "",
                role: "种植户",
                actionType: "称毛重",
            })
            this.setData({
                cropName: res.data.cropName || "",
                qualityType: res.data.qualityType,
                peelingType: res.data.peelingType,
                secondPeelingType: res.data.secondPeelingType,
                enterStockStatus: res.data.enterStockStatus,
                createTimeStr: res.data.createTime.replace("T", " "),
                farmerName: res.data.farmerName || "",
                companyName: res.data.companyName || "",
                typeList: typeList,
                operateRecord: operateRecord,
                price: res.data.transactionPrice && wx.$getYuan(res.data.transactionPrice) || "",
                managementFee: res.data.managementFee && wx.$getYuan(res.data.managementFee) || "",
                farmerMobile: res.data.farmerMobile && wx.$getMobile(res.data.farmerMobile) || "",
                grossWeight: res.data.grossWeight >= 0 ? res.data.grossWeight : "",
                tareWeight: res.data.tareWeight >= 0 ? res.data.tareWeight : "",
                carWeight: res.data.carWeight >= 0 ? res.data.carWeight : "",
                netWeight: res.data.netWeight || 0,
            })
            if (this.data.role == 2) {
                this.setData({
                    managementFee: res.data.managementFee >= 0 && wx.$getYuan(res.data.managementFee) || 0,
                })
            }
            this.setNetWeight()
        })
    },
    pickerTareWeight: function () {
        if (this.data.role == 2 || this.data.enterStockStatus > 2) {
            return
        }
        wx.navigateTo({
            url: '/pages/business/tareweight/index',
            success: (res) => {
                res.eventChannel.emit('acceptDataFromOpenerPage', {
                    typeList: this.data.typeList,
                    qualityType: this.data.qualityType,
                    secondPeelingType: this.data.secondPeelingType,
                    selectCropItem: {
                        cropName: this.data.cropName
                    },
                })
            }
        })
    },
    setNetWeight: function (event) {
        this.setData({
            netWeight: this.data.grossWeight - this.data.carWeight - this.data.tareWeight
        })
        this.setAllPrice()
    },
    setAllPrice: function (event) {
        if (this.data.enterStockStatus >= 2) {
            let {
                price,
                managementFee,
                netWeight
            } = this.data;
            this.setData({
                allPrice: Math.round(price * netWeight * 100) / 100 - Number(managementFee)
            })
        }
    },
    onChange: function (event) {
        this.setData({
            activeNames: event.detail,
        });
    },
    updateEnterStock: function (callback) {
        let params = {
            "carWeight": this.data.carWeight,
            "grossWeight": this.data.grossWeight,
            "id": this.data.id,
            "qualityType": this.data.qualityType
        }
        for (let key in params) {
            if (!params[key]) {
                wx.showToast({
                    title: this.data.errMessage[key],
                    icon: 'none',
                    duration: 2000
                })
                return
            }
        }
        if (this.data.peelingType == 2) {
            params.secondPeelingType = this.data.secondPeelingType
            params.tareWeight = this.data.tareWeight
            params.packs = this.data.typeList.filter(item => item.packWeight > 0)
        }
        if (this.data.netWeight < 0) {
            wx.showToast({
                title: "商品净重小于0，请检查重量是否正确",
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.showLoading({
            title: "正在请求数据中"
        })
        wx.$apis.updateEnterStock(params).then(res => {
            callback && callback()
        })
    },
    quotedOrder(callback) {
        if (!this.data.price) {
            wx.showToast({
                title: "修改成功",
                icon: 'none',
                duration: 2000
            })
            callback && callback()
        } else {
            let params = {
                companyId: app.globalData.userInfo.companyData.companyId,
                transactionPrice: Math.round(this.data.price * 100),
                managementFee: Math.round(this.data.managementFee * 100),
                id: [this.data.id]
            }
            wx.showLoading({
                title: "正在请求数据中"
            })
            wx.$apis.quotedOrder(params).then(res => {
                wx.showToast({
                    title: "开价成功",
                    icon: 'none',
                    duration: 2000
                })
                callback && callback()
            })
        }
    },
    submit: function () {
        const {
            enterStockStatus
        } = this.data;
        if (enterStockStatus == 1) {
            this.updateEnterStock(() => {
                wx.setStorage({
                    key: "back",
                    data: true,
                    success: function () {
                        wx.navigateBack(); //返回上一个页面
                    }
                })
            });
        } else if (enterStockStatus == 2) {
            this.updateEnterStock(() => {
                this.quotedOrder(() => {
                    wx.navigateBack()
                });
            });
        }
    },
    print: function () {
        const {id} = this.data;
        wx.showLoading({
            title: "正在请求数据中"
        })
        wx.$apis.printEnterStock(id)
    }
})